package com.tenement.controller.backend;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.entity.Admin;
import com.tenement.service.interfaces.IAdminService;
import com.tenement.utils.base.ProduceUtil;

@Controller
public class AdminController {

	private static final Log logger = LogFactory.getLog(AdminController.class);

	@Autowired
	private IAdminService iAdminService;

	/**
	 * 分页查询 前台获取传来的参数，当前页码下标，每页显示记录数
	 * 
	 * @return
	 */
	@RequestMapping(value = "getAdminListPage.action")
	public @ResponseBody JSONObject getAdminListPage(@RequestBody String data) {
		logger.info("【-分页查询-】");
		// 获取入参
		JSONObject jsonstr = JSON.parseObject(data);
		String pageIndex = jsonstr.getString("pageIndex");// 获取当前页码
		String pageSize = jsonstr.getString("pageSize"); // 获取每页显示的记录数
		String aname = jsonstr.getString("aname");
		String account = jsonstr.getString("account");
		return iAdminService.getAdminListPage(aname, account, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
	}

	/**
	 * 进入管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toAdminManageHtml.action")
	public String getAdminList(HttpServletRequest request) {
		List<Admin> adminList = iAdminService.getAdminList();
		request.setAttribute("adminList", adminList);
		return "view/backend_system/admin_list";
	}

	/**
	 * 重置管理员密码
	 * 
	 * @param a_id
	 * @return
	 */
	@RequestMapping(value = "resetAdminPwd.action")
	public @ResponseBody String resetAdminPwd(String a_id, HttpServletRequest request) {
		logger.info(a_id);
		return state.Failed;
	}

	/**
	 * 删除管理员
	 * 
	 * @param a_id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteAdmin.action")
	public @ResponseBody String deleteAdmin(String a_id, HttpServletRequest request) {
		logger.info(a_id);
		return "view/backend_system/admin_list";
	}

	/**
	 * 添加管理员
	 * 
	 * @param a_aname
	 * @param role
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addAdmin.action")
	public @ResponseBody JSONObject addAdmin(String a_aname, String role, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			if (a_aname == null || "".equals(a_aname)) {
				throw new ParameterException("【参数a_aname为空】");
			}
			if (role == null || "".equals(role)) {
				throw new ParameterException("【参数role为空】");
			}
			String a_account = ProduceUtil.getStringRandom();
			boolean b = iAdminService.addAdmin(new Admin(a_account, a_aname, Integer.parseInt(role)));
			if (b) {
				json.put("a_account", a_account);
				json.put("state", state.Success);
			} else {
				json.put("a_account", a_account);
				json.put("state", state.Failed);
			}
		} catch (ParameterException p) {
			p.printStackTrace();
		}
		return json;
	}
}
